normal text italic text bold text bold italics

headers

Header 1

Header 2

Header 3

Header 4

Header 5
Header 6

these will be how headers turn up in the table of contents. If not in order Markdwon gets confused. Also decides the size

lists

  • first item in the list
  • second item in list
  • third item in list

ordered list

  1. first
  2. second
  3. third

Block quotes

this text will be quotes

code text

a <- 10 text text text ’code example`

For an image, either link the file or put the url in

to put a hyperlink in url to original artwork

Background to Gapminder

Gapminder is an independent Swedish foundation with no political, religious or economic affiliations. Gapminder is a fact tank, not a think tank. Gapminder fights devastating misconceptions about global development. Gapminder produces free teaching resources making the world understandable based on reliable statistics. Gapminder promotes a fact-based worldview everyone can understand. Gapminder collaborates with universities, UN, public agencies and non-governmental organizations. All Gapminder activities are governed by the board. We do not award grants. Gapminder Foundation is registered at Stockholm County Administration Board

For keyboard short cut help in R go to Tools - keyboard shortcuts To insert chunks of R code go to insert at the top and click R. On a mac can use ctrl option I

a <- 10
b <- 15
a*b
## [1] 150

chunk uses There are a lot of things you can do in a code chunk: you can produce text output, tables, or graphics. You have fine control over all these output via chunk options, which can be provided inside the curly braces
For example, you can choose hide text output via the chunk option results = ‘hide’, or set the figure height to 4 inches via fig.height = 4. Each chunk needs a seperate name if you do name it Look at cheat sheet for chunk control options Chunk options are separated by commas, e.g., {r, chunk-label, results='hide', fig.height=4}

eval= false at end of code means that the output of the code chunk won’t be included see website for more options

can use set up chunk to set settings for the whole document (opts_chunk)

Installation instructions

So to run this we need to install a few packages:

  1. ggplot2
  2. skimr
  3. DT

To instal these we use

install.packages(c("skimr", "ggplot2", "DT"))

Displaying data

Data can be read in ro Markdown in different ways you can print data.frames

data(airquality)
head(airquality)
##   Ozone Solar.R Wind Temp Month Day
## 1    41     190  7.4   67     5   1
## 2    36     118  8.0   72     5   2
## 3    12     149 12.6   74     5   3
## 4    18     313 11.5   62     5   4
## 5    NA      NA 14.3   56     5   5
## 6    28      NA 14.9   66     5   6

You can use tibble instead of data and it looks nicer to the reader tibble is from tidyverse so new R. Tibble is a posh data frame that prints nicely Only prints the top 10 but tells you more info on the data not shown

library(tibble)
## Warning: package 'tibble' was built under R version 3.5.3
as_tibble(airquality)
## # A tibble: 153 x 6
##    Ozone Solar.R  Wind  Temp Month   Day
##    <int>   <int> <dbl> <int> <int> <int>
##  1    41     190   7.4    67     5     1
##  2    36     118   8      72     5     2
##  3    12     149  12.6    74     5     3
##  4    18     313  11.5    62     5     4
##  5    NA      NA  14.3    56     5     5
##  6    28      NA  14.9    66     5     6
##  7    23     299   8.6    65     5     7
##  8    19      99  13.8    59     5     8
##  9     8      19  20.1    61     5     9
## 10    NA     194   8.6    69     5    10
## # ... with 143 more rows

Displaying knitr::kable() tables

We can use other packages to create html tables from our data. The simplest is to use the knitr::kable() function. This uses kable on just the head of airquality. This makes a proper html table

library(knitr)
## Warning: package 'knitr' was built under R version 3.5.3
data(airquality)
kable(head(airquality), caption = "New York Air Quality Measurements")
New York Air Quality Measurements
Ozone Solar.R Wind Temp Month Day
41 190 7.4 67 5 1
36 118 8.0 72 5 2
12 149 12.6 74 5 3
18 313 11.5 62 5 4
NA NA 14.3 56 5 5
28 NA 14.9 66 5 6

Interactive table

Displaying interactive DT::datatable() tables You can display interactive html tables using function DT::datatable():

library(DT)
## Warning: package 'DT' was built under R version 3.5.3
data(airquality)
datatable(airquality, caption = "New York Air Quality Measurements")

This is really useful for sending to collaborators etc Allows all the data to be present in a useful way

Summarising data with skimr::skim()

Fuction skimr::skim() provides a simple approach to displaying summary statistics that can be quickly skimmed quickly to understand data.

skimr::skim(airquality)
## Skim summary statistics
##  n obs: 153 
##  n variables: 6 
## 
## -- Variable type:integer -------------------------------------------------------------------------------------------------------------------------------------------
##  variable missing complete   n   mean    sd p0    p25   p50    p75 p100
##       Day       0      153 153  15.8   8.86  1   8     16    23      31
##     Month       0      153 153   6.99  1.42  5   6      7     8       9
##     Ozone      37      116 153  42.13 32.99  1  18     31.5  63.25  168
##   Solar.R       7      146 153 185.93 90.06  7 115.75 205   258.75  334
##      Temp       0      153 153  77.88  9.47 56  72     79    85      97
##      hist
##  <U+2587><U+2587><U+2587><U+2587><U+2586><U+2587><U+2587><U+2587>
##  <U+2587><U+2587><U+2581><U+2587><U+2581><U+2587><U+2581><U+2587>
##  <U+2587><U+2586><U+2583><U+2583><U+2582><U+2581><U+2581><U+2581>
##  <U+2583><U+2583><U+2583><U+2583><U+2585><U+2587><U+2587><U+2583>
##  <U+2582><U+2582><U+2583><U+2586><U+2587><U+2587><U+2583><U+2583>
## 
## -- Variable type:numeric -------------------------------------------------------------------------------------------------------------------------------------------
##  variable missing complete   n  mean   sd  p0 p25 p50  p75 p100     hist
##      Wind       0      153 153  9.96 3.52 1.7 7.4 9.7 11.5 20.7 <U+2581><U+2583><U+2587><U+2587><U+2585><U+2585><U+2581><U+2581>

Plots

By default, figures produced by R code will be placed immediately after the code chunk they were generated from. Let’s use ggplot2 to have a look to the relationship between a couple of variables

interactive plots with plotly

Wraps nicely around plotting library ggplot2